﻿using System.IO;
using System.Text;
using System;

namespace OpenGameEngine.Net
{
    public partial class GameStream : Stream
    {
        public GameStream(Stream baseStream)
            : this(baseStream, Encoding.BigEndianUnicode)
        {
        }

        public GameStream(Stream baseStream, Encoding encoding)
        {
            BaseStream = baseStream;
            Encoding = encoding;
        }

        public Stream BaseStream { get; private set; }

        public override bool CanRead { get { return BaseStream.CanRead; } }

        public override bool CanSeek { get { return BaseStream.CanSeek; } }

        public override bool CanWrite { get { return BaseStream.CanWrite; } }

        public override void Flush()
        {
            BaseStream.Flush();
        }

        public override long Length
        {
            get { return BaseStream.Length; }
        }

        public override long Position
        {
            get { return BaseStream.Position; }
            set { BaseStream.Position = value; }
        }

        public override int Read(byte[] buffer, int offset, int count)
        {
            int returnReadLenght = 0;
            IAsyncResult ar = BaseStream.BeginRead(buffer, offset, count, null, null);
            returnReadLenght = BaseStream.EndRead(ar);
            return returnReadLenght;
        }

        public override long Seek(long offset, SeekOrigin origin)
        {
            return BaseStream.Seek(offset, origin);
        }

        public override void SetLength(long value)
        {
            BaseStream.SetLength(value);
        }

        public override void Write(byte[] buffer, int offset, int count)
        {
            IAsyncResult ar = BaseStream.BeginWrite(buffer, offset, count, null, null);
            BaseStream.EndWrite(ar);
        }
    }
}
